<template>
  <div class="search-result">
    <!-- 导航栏 -->
    <van-nav-bar
      :title="$route.query.keyword+'的搜索结果'"
      left-arrow
      fixed
      @click-left="$router.back()"
    />
    <!-- /导航栏 -->

    <!-- 文章列表 -->
    <van-list
      v-model="loading"
      class="article-list"
      :finished="finished"
      finished-text="嘻嘻，没有了哦"
      @load="onLoad"
    >
      <van-cell
        v-for="item in searchResultList"
        :key="item.art_id"
        :title="item.aut_name"
        @click="$router.push(`/articleDetail/${item.art_id}`)"
      />
    </van-list>
  </div>
</template>

<script>
import { reqSearchResultList } from '@/api/search'

export default {
  name: 'SearchResult',
  data() {
    return {
      list: [],
      loading: false,
      finished: false,
      page: 1,
      searchResultList: []
    }
  },

  methods: {
    async onLoad() {
      // 异步更新数据
      const res = await reqSearchResultList(this.$route.query.keyword, this.page)
      this.page++
      this.searchResultList = [...this.searchResultList, ...res.data.results]
      this.loading = !this.loading
      if (this.searchResultList.length > 50 || res.data.results.length < 50) {
        this.finished = !this.finished
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.search-result {
  .article-list {
    margin-top: 39px;
  }
}
</style>
